<?php

class Business_detail extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'Business_detail':
	 * @var integer $id
	 * @var integer $fk_customer_id
	 * @var integer $business_type
	 * @var integer $carrier_type
	 * @var integer $truck_times
	 * @var double $tonnage
	 * @var double $receivable_amount
	 * @var double $payable_amount
	 * @var double $disparity
	 * @var double $disparity_ratio
	 * @var double $loss_amount
	 * @var string $remark1
	 * @var string $remark2
	 * @var string $occur_time
	 * @var string $create_time
	 * @var string $update_time
	 * @var string $create_user
	 * @var string $update_user
	 * @var integer $state
	 */
	
	private $_business_type=array(0=>'运输',1=>'仓储',2=>'装卸');
	private $_carrier_type=array(0=>'自有车',1=>'第三方');
	public $customer_name;
	public $disparity_sum;
	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'business_detail';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('remark1','length','max'=>255),
			array('remark2','length','max'=>255),
			array('create_user','length','max'=>30),
			array('update_user','length','max'=>30),
			//array('disparity_ratio', 'numerical','min'=>0,'max'=>100),
			array('business_type, carrier_type, truck_times', 'numerical', 'integerOnly'=>true),
			array('tonnage, receivable_amount, payable_amount, disparity, disparity_ratio, loss_amount', 'numerical'),
			array('receivable_amount, payable_amount, disparity,loss_amount', 'match','pattern' => '/^-?\d{1,16}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多16位，小数位2位'),
			array('tonnage', 'match','pattern' => '/^\d{1,8}(\.\d{1,3})?$/','message'=>'{attribute} 整数位最多8位，小数位3位'),
			array('disparity_ratio', 'match','pattern' => '/^-?\d{1,3}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多2位，小数位2位'),
			array('occur_time', 'type','type'=>'date','message'=>'{attribute} 必须为日期型(年-月-日)'),
			array('business_type,carrier_type,truck_times,tonnage,receivable_amount,payable_amount,disparity,
			disparity_ratio,loss_amount,occur_time,customer_name', 'required'),
		);
	}

	/**
	 * Prepares attributes before performing validation.
	 */
	protected function beforeValidate($on)
	{
		if($this->isNewRecord){
			$this->state=0;
			$this->create_time=date('Y-m-d G:i:s');
			$this->create_user=Yii::app()->session['true_name'];
		}
		$this->update_time=date('Y-m-d G:i:s');
		$this->update_user=Yii::app()->session['true_name'];
		return true;
	}
	
	protected function afterValidate($on)//在验证之前将无需用户输入的字段写入
	{	
		if($this->hasErrors()) return;
		$this->disparity_ratio/=100;
		return true;
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => '索引号',
			'fk_customer_id' => '客户id',
			'business_type' => '业务类型',//0运输 1仓储 2装卸
			'carrier_type' => '承运方',//0自有车 1第三方
			'truck_times' => '车次',
			'tonnage' => '吨位',
			'receivable_amount' => '应收(元)',
			'payable_amount' => '应付(元)',
			'disparity' => '差价(元)',
			'disparity_ratio' => '差价率(%)',
			'loss_amount' => '液奶补损',
			'remark1' => '备注1',
			'remark2' => '备注2',
			'occur_time' => '业务时间',
			'create_time' => '创建时间',
			'update_time' => '更新时间',
			'create_user' => '录入员',
			'update_user' => '修改人',
			'state' => '状态',
			'customer_name' => '客户名称',
			'disparity_sum' => '差价/总差价'
		);
	}
	
	public function excelAttributes(){//导出excel所需的字段名、列号、宽度
		return array(
			'customer_name'=>array('B',40),
			'truck_times'=>array('C',12,'sum'=>true),
			'tonnage'=>array('D',12,'sum'=>true),
			'receivable_amount'=>array('E',12,'sum'=>true),
			'payable_amount'=>array('F',12,'sum'=>true),
			'disparity'=>array('G',12,'sum'=>true),
			'loss_amount'=>array('H',12,'sum'=>true),
			'disparity_ratio'=>array('I',12,'sum'=>'FORMAT(sum(disparity)/sum(receivable_amount),4)'),
			'disparity_sum'=>array('J',12),
		);
	}
	
	public function excelAnnualAttributes(){//导出excel所需的字段名、列号、宽度
		return array(
			'truck_times',
			'tonnage',
			'receivable_amount',
			'payable_amount',
			'disparity',
			'disparity_ratio'
		);
	}
	
	public function getBusinessTypeNames(){
		return $this->_business_type;
	}
	
	public function getBusinessTypeName($n=null){
		if($n==null&&$this->business_type==null) return '';
		return $this->_business_type[$n==null?$this->business_type:$n];
	}
	
	public function getCarrierTypeNames(){
		return $this->_carrier_type;
	}
	
	public function getCarrierTypeName($n=null){
		if($n==null&&$this->carrier_type==null) return '';
		return $this->_carrier_type[$n==null?$this->carrier_type:$n];
	}
	
	public function safeAttributes(){
		$params=parent::safeAttributes();
		$params[]='customer_name';
		return $params;
	}
}